
package com.jic.interaction.cardCouponPlatform.common.utils;

import com.jic.interaction.hereWeGo.common.utlis.EncryDecryPtionUtil;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.Response;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * http请求通用类
 *
 * @author Administrator
 */
@Component
public class CardCouponPlatformHttpUtil {

    private static final Log log = LogFactory.getLog(CardCouponPlatformHttpUtil.class.getName());

    private static String httpKey;



    public static String doRequestPost(String url, String params) throws Exception, IOException, ExecutionException, InterruptedException {
        log.info("请求地址: " + url);
        log.info("请求参数: " + params);
        AsyncHttpClient http = new AsyncHttpClient();
        AsyncHttpClient.BoundRequestBuilder builder = http.preparePost(url);
        builder.setBodyEncoding("UTF-8");
        builder.addHeader("Content-Type","application/json");
        builder.setBody(params);
        Future<Response> f = builder.execute();
        String body = (f.get()).getResponseBody("UTF-8");
        log.info("返回参数: " + body);
        http.close();
        return body;
    }
    /*
        请求路径 请求体
        jsonData：业务字段，String类型的 json；
     */
    public String requestPath(String url, String params) throws Exception, IOException, ExecutionException, InterruptedException {
        String res;
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        long ts = date.getTime();
        res = String.valueOf(ts);

        return doRequestPost(url,params);
    }

    /*
        将字符数组转换为字符串
     */
    private static String bytesToHex(byte[] digest) {
        String join = StringUtils.join(digest);
        return join;
    }


}

